<template>
	<view class="u-wrap">
		<view class="u-search-box">
			<u-search placeholder="输入商品名称" v-model="keyword" @custom="searchGoods" @clear="clearSearch"></u-search>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<block v-for="(item,index) in categories" :key="item.id">
					<view v-for="c in item.children" :key="c.id"  class="u-tab-item" :class="[current==c.id ? 'u-tab-item-active' : '']" :data-current="c.id" @tap.stop="swichMenu(c.id)">
					<text class="u-line-1">{{c.name}}</text>
				</view>
					
				</block>
				
			</scroll-view>
			<block>
				<scroll-view scroll-y class="right-box" @scrolltolower="scrollEvent">
					<view class="page-view">
						<view class="class-item">
							<!-- <view class="item-title">
								<text>{{item.name}}</text>
							</view> -->
							<view class="item-container">
								<navigator
								class="thumb-box  u-p-b-40 u-p-t-40"
								v-for="(goods, goodsIndex) in goodsList"
								:key="goodsIndex"
								:url="'/pages/goods/show?id='+goods.id"
								><!-- :url="`/pages/goods/goods-detail/goods-detail?id=${goods.id}`" -->
									<image class="item-menu-image" :src="goods.cover_url" mode=""></image>
									<view class="item-menu-name">{{goods.title}}</view>
								</navigator>
								<view v-if="goodsList.length===0" class="u-flex-1 u-p-t-80 u-p-b-80">
									<u-empty text="暂无数据" mode="list"></u-empty>
								</view>
								
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyword:'',//搜索关键词
				categories: [],
				goodsList:[],
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				page:1,
				isLastPage:false //用来判断是否是最后一页
			}
		},
		onLoad(){
			this.getData()
		},
		computed: {
			
		},
		methods: {
			async getData(){
				const params = {
					page:this.page,
					title:this.keyword
				}
				// 判断是否有商品分类ID
				if(this.current) {
					params.category_id=this.current
				}
				const res = await this.$u.api.goodsList(params)
				this.categories = res.categories
				// this.goodsList = res.goods.data
				// 有分页时 应该用展开运算符来写 将结果展开
				this.goodsList = [...this.goodsList,...res.goods.data]
				this.isLastPage = res.goods.next_page_url ? false : true
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(cid) {
				if(cid == this.current) return ;
				this.current = cid;
				this.initData()
			},

			// 搜索商品
			searchGoods(){
				this.initData()
			},
			// 清除搜索商品
			clearSearch(){
				this.keyword = ''
				this.initData()
			},
			// 重置商品和分页
			initData(){
				this.page = 1
				this.goodsList = []
				this.getData();
			},
			
			// 右侧区域滚动到底部 加载更多
			scrollEvent(e){
				// 如果最后一页 则返回 不执行
				if(this.isLastPage)return
				// 重新请求数据 带上分页参数
				this.page = this.page + 1
				this.getData()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		/* #ifdef H5*/
		height: calc(100vh - var(--window-top) - 100rpx);
		/* #endif*/
		/* #ifndef H5*/
		height: 100vh;
		/* #endif*/
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}
	
	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}
	
	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid $u-type-primary;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}
	
	.right-box {
		background-color: rgb(250, 250, 250);
	}
	
	.page-view {
		padding: 16rpx;
	}
	
	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}
	
	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}
	
	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}
	
	.item-container {
		display: flex;
		flex-wrap: wrap;
	}
	
	.thumb-box {
		width: 45%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}
	
	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}
</style>
